package io.zhifei.modules.sys.system.service.customer.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.zhifei.common.exception.RRException;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.common.utils.ToolUtils;
import io.zhifei.modules.sys.system.dao.customer.CustomerDao;
import io.zhifei.modules.sys.system.entity.customer.CustomerEntity;
import io.zhifei.modules.sys.system.form.SysDictParam;
import io.zhifei.modules.sys.system.form.SysDictResult;
import io.zhifei.modules.sys.system.form.customer.CustomerExcelResult;
import io.zhifei.modules.sys.system.form.customer.CustomerFollowResult;
import io.zhifei.modules.sys.system.form.customer.CustomerParam;
import io.zhifei.modules.sys.system.form.customer.CustomerResult;
import io.zhifei.modules.sys.system.service.SysDictService;
import io.zhifei.modules.sys.system.service.customer.CustomerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author duke
 * @since 2020-12-09
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerDao, CustomerEntity> implements CustomerService {

    @Resource
    private SysDictService dictService;

    @Override
    public CustomerEntity add(CustomerParam param) {
        getSeaStatus(param);
        CustomerEntity entity = getEntity(param);
        this.save(entity);
        return entity;
    }

    @Override
    public void delete(CustomerParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(CustomerParam param) {
        getSeaStatus(param);
        CustomerEntity oldEntity = getOldEntity(param);
        CustomerEntity newEntity = getEntity(param);
        ToolUtils.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CustomerResult getEditDetail(CustomerParam param) {
        return this.baseMapper.getEditDetail(param);
    }

    @Override
    public CustomerResult getViewDetail(CustomerParam param) {
        return this.baseMapper.getViewDetail(param);
    }

    @Override
    public List<CustomerResult> getList(CustomerParam param) {
        return this.baseMapper.getList(param);
    }

    @Override
    public PageUtils getPageList(Page page, CustomerParam param) {
        IPage pageList = this.baseMapper.getPageList(page, param);
        return new PageUtils(pageList);
    }

    @Override
    public List<CustomerExcelResult> getExcelList(CustomerParam param) {
        List<CustomerExcelResult> excelList = this.baseMapper.getExcelList(param);
        excelList.forEach(result ->{
            CustomerFollowResult followResult = this.baseMapper.getLastCustomerFollowDetail(result.getCustomerId());
            if (null != followResult){
                result.setNextFollowTime(DateUtil.format(followResult.getCreateTime(), DateFormat.getDateTimeInstance()));
                result.setIntervalDays(followResult.getIntervalDays());
                result.setFollowContent(followResult.getContent());
            }

        });
        return excelList;
    }

    @Override
    public void updateCustomerStatus(long userId,Long followId) {
        this.baseMapper.updateCustomerStatus(userId,followId);
    }

    @Override
    public Boolean getPhoneStatus(String tel) {
         QueryWrapper<CustomerEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel",tel);
        queryWrapper.eq("is_deleted",0);
        CustomerEntity customerEntity = this.getOne(queryWrapper);
        if (null != customerEntity){
            return false;
        }else {
            return true;
        }
    }


    private Serializable getKey(CustomerParam param) {
        return param.getCustomerId();
    }

    private CustomerEntity getOldEntity(CustomerParam param) {
        return this.getById(getKey(param));
    }

    private CustomerEntity getEntity(CustomerParam param) {
        CustomerEntity entity = new CustomerEntity();
        ToolUtils.copyProperties(param, entity);
        return entity;
    }

    /**
     * 验证是否转入公海
     *
     * @param param 客户信息
     * @return
     */
    private CustomerParam getSeaStatus(CustomerParam param) {
        //验证用户电话是否重复
        CustomerResult customerResult = this.baseMapper.getViewDetail(param);
        if (null != customerResult && !customerResult.getCustomerId().equals(param.getCustomerId())) {
            throw new RRException("客户电话不能重复！");
        }
        if (null != param.getStatus()) {
            //获取客户状态信息
            SysDictResult dictResult = dictService.getDetail(SysDictParam.builder().id(param.getStatus()).build());
            //当类型为直接进入公海时，修改客户的公海状态
            if (3 == dictResult.getSeaType()) {
                param.setIsToSysseas(1);
                param.setAddTime(new Date());
                param.setFollowId(0L);
                param.setAddReason("直接进入公海");
            }
        }
        return param;
    }

}
